#include "question3.h"
#include <QPainter>
#include "mypushbutton.h"
#include <QDebug>
#include <QTimer>
#include "myflower.h"
#include "dataconfig.h"
#include <QMessageBox>

Question3::Question3(QWidget *parent) : QMainWindow(parent)
{
    this->setFixedSize(1200,800);

    this->setWindowIcon(QPixmap(":/pictures/title.png"));

    this->setWindowTitle("问题");

    MyPushButton * backBtn = new MyPushButton(":/pictures/backbutton.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    dataConfig config;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            this->gameArray[i][j]=config.mData[1][i][j];
        }
    }

    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            QString str;
            if(this->gameArray[i][j]==1)
            {
                str=":/pictures/yellow.png";
            }
            else
            {
                str=":/pictures/red.png";
            }

            MyFlower * flower = new MyFlower(str) ;
            flower->setParent(this);
            flower->move(300+150*i,200+150*j);

            flower->posX = i;
            flower->posY = j;
            flower->flag = this->gameArray[i][j];

            flowerbtn[i][j] = flower;

            connect(flower,&MyFlower::clicked,[=](){
               flower->changeFlag();
               this->gameArray[i][j] = (this->gameArray[i][j] == 0 ? 1:0);

               if(flower->posX + 1<=3)
               {
                   flowerbtn[flower->posX + 1][flower->posY]->changeFlag();
                   this->gameArray[flower->posX + 1][flower->posY] = (this->gameArray[flower->posX + 1][flower->posY] == 0 ? 1:0);
               }
               if(flower->posX - 1>=0)
               {
                   flowerbtn[flower->posX - 1][flower->posY]->changeFlag();
                   this->gameArray[flower->posX - 1][flower->posY] = (this->gameArray[flower->posX - 1][flower->posY] == 0 ? 1:0);
               }
               if(flower->posY + 1<=3)
               {
                   flowerbtn[flower->posX][flower->posY + 1]->changeFlag();
                   this->gameArray[flower->posX][flower->posY + 1] = (this->gameArray[flower->posX][flower->posY + 1] == 0 ? 1:0);
               }
               if(flower->posY + 1>=0)
               {
                   flowerbtn[flower->posX][flower->posY - 1]->changeFlag();
                   this->gameArray[flower->posX][flower->posY - 1] = (this->gameArray[flower->posX][flower->posY - 1] == 0 ? 1:0);
               }

               this->isWin = true;
               for(int i=0;i<4;i++)
               {
                   for(int j=0;j<4;j++)
                   {
                       if((flowerbtn[i][j]->flag == true) & ((i==0) | (i==3) | (j==0) | (j==3)))
                       {
                           this->isWin=false;
                           break;
                       }
                       if((flowerbtn[i][j]->flag == false) & ((i!=0) & (i!=3) & (j!=0) & (j!=3)))
                       {
                           this->isWin=false;
                           break;
                       }
                   }
               }
               if(isWin == true)
               {
                   QMessageBox::information(this,"恭喜","恭喜挑战成功！数字就在图案中！");
               }
            });
        }
    }

    connect(backBtn,&MyPushButton::clicked,[=](){
        backBtn->zoom1();
        backBtn->zoom2();

        QTimer::singleShot(500,this,[=](){
            emit this->q3b();
        });
    });
}

void Question3::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/pictures/mi3.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
}
